﻿#region

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Pob.GameObjects;

#endregion

namespace Pob.ScreenManagement
{
    public delegate void ComponentAddedEvent();

    public delegate void ComponentRemovedEvent(GameComponent component);

    public enum ScreenState
    {
        Hidden, 

        InTransition, 

        Active
    }

    public class ScreenBase : DrawableGameComponent
    {
        #region Fields

        private readonly GameComponentCollection _components;

        #endregion

        #region Constructors and Destructors

        protected ScreenBase(Game game)
            : base(game)
        {
            _components = Game.Components;
            MyComponents = new List<GameComponent>();
            Settings = SettingsManager.CurrentInstance.Settings;
            FpsCounter = new FpsCounter(game, GameContent.FontFpsCounter);
        }

        #endregion

        #region Public Events

        public event ComponentAddedEvent ComponentAdded;

        public event ComponentRemovedEvent ComponentRemoved;

        #endregion

        #region Public Properties

        public bool IsExiting { get; set; }

        public bool IsPopup { get; set; }

        public List<GameComponent> MyComponents { get; private set; }

        public ScreenManager ScreenManager { get; set; }

        public ScreenState ScreenState { get; set; }

        #endregion

        #region Properties

        protected Background Background { get; set; }

        protected FpsCounter FpsCounter { get; set; }

        protected Settings Settings { get; set; }

        #endregion

        #region Public Methods and Operators

        public void AddToMyComponents(GameComponent component)
        {
            MyComponents.Add(component);
            OnComponentAdded();
        }

        public void AddRangeToMyComponents(IEnumerable<GameComponent> component)
        {
            foreach (var gameComponent in component)
            {
                AddToMyComponents(gameComponent);
            }
        }

        public void RemoveFromMyComponents(GameComponent component)
        {
            MyComponents.Remove(component);
            OnComponentRemoved(component);
        }

        public void Pause()
        {
            Enabled = false;
            foreach (var component in MyComponents)
            {
                component.Enabled = false;
            }
        }

        public void Resume()
        {
            Enabled = true;
            foreach (var component in MyComponents)
            {
                component.Enabled = true;
            }
        }

        #endregion

        #region Methods

        protected void Add(GameComponent component)
        {
            _components.Add(component);
        }

        protected void AddRange(IEnumerable<GameComponent> list)
        {
            foreach (var component in list)
            {
                _components.Add(component);
            }
        }

        protected void Remove(GameComponent component)
        {
            _components.Remove(component);
        }

        protected override void LoadContent()
        {
            if (Settings.FpsCounter)
            {
                _components.Add(FpsCounter);
            }

            base.LoadContent();
        }

        private void OnComponentAdded()
        {
            if (ComponentAdded != null)
            {
                ComponentAdded();
            }
        }

        private void OnComponentRemoved(GameComponent component)
        {
            if (ComponentRemoved != null)
            {
                ComponentRemoved(component);
            }
        }

        #endregion
    }
}